import pymysql
import pandas as pd


def test_method(sql: str, df_name: str):
    host = "linux.server"
    user = "root"
    password = "Chenhao$123456"
    db = "telco_db"
    port = 3306
    connection = pymysql.connect(host=host, user=user, password=password, db=db, port=int(port), charset="utf8")
    try:
        # 执行sql并保存为全局变量
        df = pd.read_sql(sql, connection)
        print(df)
        globals()[df_name] = df
        # print(df_name)
        return f"成功创建pandas对象{df_name}，包含从mysql中提取的数据。"
    except Exception as e:
        return f"提取数据失败：{e}"
    finally:
        connection.close()


test_method(sql="select * from tb_user", df_name="user_info")
